package com.kws.system.service.payment.impl;

import cn.hutool.json.JSONUtil;
import com.kws.common.core.exception.ServiceException;
import com.kws.common.core.utils.StringUtils;
import com.kws.system.domain.info.RemitAccountRespondInfo;
import com.kws.system.domain.payment.rq.CreateRemitAccountRq;
import com.kws.system.domain.vo.CreateSeparateAccountRq;
import com.kws.system.service.payment.UnionPaySeparateAccountService;
import com.kws.system.util.HttpClientUtil;
import com.kws.system.util.SuperUtil;
import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

/**
 * <b>UnionPaySeparateAccountServiceImpl</b>
 *
 * @description: UnionPaySeparateAccountServiceImpl <br>
 * @date: 2024/12/20 11:06 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class UnionPaySeparateAccountServiceImpl implements UnionPaySeparateAccountService {
    private static final Logger logger = LoggerFactory.getLogger(UnionPaySeparateAccountServiceImpl.class);

    @Autowired
    private SuperUtil superUtil;

    @Value("${unionPay.CHANNEL_ID}")
    public static String channelId;

    @Value("${unionPay.URL_PRE}")
    public String preURL;

    @Value("${unionPay.VER_NO}")
    public String verNo;

    @Value("${unionPay.GROUP_ID}")
    public String groupId;

    @Value("${unionPay.FAN_RPIVATE_KEY_PATH}")
    public String rpivateKeyPah;

    @Value("${unionPay.FAN_RPIVATE_KEY_PWD}")
    public String rpivateKeyPwd;

    @Value("${unionPay.FAN_RPUBLIC_KEY_PATH}")
    public String rpublicKeyPath;

    @Override
    public boolean chargeAccordingToAmount(CreateRemitAccountRq rq) throws Exception {
        rq.setTransCode("202002");
        Map sign = superUtil.sign(rq);
        logger.info("获取返回结果:{}"+ JSONUtil.parseObj(sign));
        String content = JSONUtil.toJsonStr(sign);
        String respond = superUtil.post(preURL + rq.getTransCode(), content,"utf-8");
        logger.info("获取划账返回结果"+respond);
        RemitAccountRespondInfo info = JSONUtil.toBean(JSONUtil.parseObj(respond), RemitAccountRespondInfo.class);
        if (null != info){
            if ("99999999".equals(info.getRespCode())){

                return true;
            }else {
                throw new ServiceException(info.getRespMsg());
            }
        }
        return false;
    }

    @Override
    public boolean createSeparateAccount(CreateSeparateAccountRq rq) throws Exception {
        logger.info("分账请求参数:{}"+JSONUtil.parseObj(rq));
        if (StringUtils.isEmpty(rq.getCardNo())){
            throw new ServiceException("分账卡号不能为空!",30001);
        }
        byte[] bytes = rq.getCardNo().getBytes();
        byte[] encrpty = superUtil.encrpty(bytes, superUtil.getPubKey(), "RSA");
        String cardNo = new String(encrpty);
        rq.setCardNo(cardNo);
        rq.setTransCode("202003");
        Map sign = superUtil.sign(rq);
        String content = JSONUtil.toJsonStr(sign);
        String respond = superUtil.post(preURL + rq.getTransCode(), content,"utf-8");
        logger.info("获取分账返回结果"+respond);
        RemitAccountRespondInfo info = JSONUtil.toBean(JSONUtil.parseObj(respond), RemitAccountRespondInfo.class);
        if (null != info){
            if ("99999999".equals(info.getRespCode())){

                return true;
            }else {
                throw new ServiceException(info.getRespMsg());
            }
        }
        return false;
    }
}
